package org.squirrel.modules.bigscreen.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.squirrel.common.api.vo.Result;
import org.squirrel.common.system.base.controller.SquirrelController;
import org.squirrel.modules.bigscreen.entity.GaeaFile;
import org.squirrel.modules.bigscreen.service.GaeaFileService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 文件
 * 
 * @author leixiangjian
 *
 */
@Slf4j
@RestController
@RequestMapping("/file")
@Api(value = "/file", tags = "文件")
public class GaeaFileController extends SquirrelController<GaeaFile, GaeaFileService> {
    @Autowired
    private GaeaFileService gaeaFileService;

    /**
     * 更新文件
     * 
     * @param file
     * @return
     */
    @ApiOperation(value = "更新文件", notes = "更新文件")
    @PostMapping("/upload")
    public Result<?> upload(@RequestParam("file") MultipartFile file) {
    	try {
    		GaeaFile gaeaFile = gaeaFileService.upload(file);
    		return Result.OK("更新文件成功", gaeaFile);
    	}catch(Exception e) {
    		log.error("更新文件异常", e);
			return Result.error("更新文件失败,原因:" + e.getMessage());
    	}
    }

    /**
     * 下载文件
     * 
     * @param request
     * @param response
     * @param fileId
     * @return
     */
    @ApiOperation(value = "下载文件", notes = "下载文件")
    @GetMapping(value = "/download/{fileId}")
    public Result<?> download(HttpServletRequest request, HttpServletResponse response, @PathVariable("fileId") String fileId) {
    	try {
    		ResponseEntity<byte[]> responseEntity =  gaeaFileService.download(request, response, fileId);
    		return Result.OK("下载文件成功", responseEntity);
    	}catch(Exception e) {
    		log.error("下载文件异常", e);
			return Result.error("下载文件失败,原因:" + e.getMessage());
    	}
    }
}
